<?php
/*
 * @Description ：redis操作日志类
 * @filename    : RedisLog.php
 * @author      : liuqingwei
 * @date        : 2019.07.31
 * @Copyright (c) 2015 Julive
 */
namespace yii\log;


class RedisLog
{
    
    public $redis;
    
    public $list_key;
    
    const REDIS = 'redis';
    
    /**
     * 初始化key
     *
     * @param string $app 项目名称
     *
     * @return mixed
     */
    public function __construct($app='app')
    {
        //检查redis.so是否存在
        if (!extension_loaded(self::REDIS))
        {
            return;
        }
    
        $config = [
            'host'    => '127.0.0.1',
            'port'    => 6379,
            'timeout' => 30,
        ];
    
        try {
            $this->redis = new \Redis();
            $res = $this->redis->connect($config['host'],$config['port'],$config['timeout']);
        } catch (\Exception $e) {
            return;
        }
        if ($res === false)
        {
            return;
        }
    
        $this->list_key = sprintf("redis_%s_log",$app);
    }
    
    /**
     * 写日志到redis
     */
    public function writeLog($msg)
    {
       
        $res = $this->redis->lPush($this->list_key,$msg);

        if ($res === 1)
        {
            return ['code' => 0,'msg'=>'success'];
        }

        return ['code' => 1,'msg'=>'success'];
    }
    
    /**
     * 扩展方法
     *
     * @param  string $name         方法名
     * @param  array  $arguments    参数
     *
     * @return mixed
     */
    public function __call($name, $arguments)
    {
        return $this->redis->$name(...$arguments);
    }
}